package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车业务
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        //根据条件查询购物车的数据
        ShoppingCart shoppingCart = shoppingCartMapper.selectShoppingCart(shoppingCartDTO,userId);

        if (ObjectUtil.isNotNull(shoppingCart)){   //如果购物车有数据，修改数量
            int count = 1;
            shoppingCartMapper.updateNumber(shoppingCart.getId(),count);
        }else {  //购物车没数据
            shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            if (shoppingCartDTO.getSetmealId() == null){  //判断是菜品还是套餐
                Dish dish = dishMapper.getDishById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCartMapper.insertShoppingCart(shoppingCart);
            }else {  //套餐，封装ShoppingCart插入一条数据
                Setmeal setmeal = setmealMapper.getSetmealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCartMapper.insertShoppingCart(shoppingCart);
            }
        }
    }

    /**
     * 根据userid查看用户购物车
     * @return
     */
    public List<ShoppingCart> getShoppingCart() {
        return shoppingCartMapper.selectShoppingCartByUserId(BaseContext.getCurrentId());
    }

    /**
     * 清空用户购物车
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteShoppingCart(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void DeleteAnItemFromShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = shoppingCartMapper.selectShoppingCart(shoppingCartDTO, userId);
        if (ObjectUtil.isNull(shoppingCart)){
            return;
        }
        if (shoppingCart.getNumber() > 1){
            int count = -1;
            shoppingCartMapper.updateNumber(shoppingCart.getId(),count);
        }else {
            shoppingCartMapper.deleteShoppingCartById(shoppingCart.getId());
        }
    }
}
